package com.siwei.eqi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.siwei.eqi.common.BatchStatusEnum;
import com.siwei.eqi.pojo.dto.DeliveryTaskBatchDTO;
import com.siwei.eqi.pojo.entity.DeliveryTask;
import com.siwei.eqi.pojo.entity.DeliveryTaskBatch;
import com.siwei.eqi.mapper.DeliveryTaskBatchMapper;
import com.siwei.eqi.pojo.entity.DeliveryTaskBatchResult;
import com.siwei.eqi.pojo.vo.BatchResultVO;
import com.siwei.eqi.pojo.vo.DelivertTaskInfoVO;
import com.siwei.eqi.pojo.vo.DeliveryTaskBatchInfoVO;
import com.siwei.eqi.pojo.vo.DeliveryTaskBatchVO;
import com.siwei.eqi.service.IDeliveryTaskBatchResultService;
import com.siwei.eqi.service.IDeliveryTaskBatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siwei.eqi.service.IDeliveryTaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 汇交批次表 服务实现类
 * </p>
 *
 * @author hzl
 * @since 2025-04-02
 */
@Service
public class DeliveryTaskBatchServiceImpl extends ServiceImpl<DeliveryTaskBatchMapper, DeliveryTaskBatch> implements IDeliveryTaskBatchService {

    @Autowired
    private IDeliveryTaskService taskService;

    @Autowired
    private IDeliveryTaskBatchResultService resultService;

    @Override
    public List<DeliveryTaskBatchVO> queryList(String taskId) {
        List<DeliveryTaskBatch> list = baseMapper.selectList(new LambdaQueryWrapper<DeliveryTaskBatch>()
                .eq(DeliveryTaskBatch::getTaskId, taskId));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(list, DeliveryTaskBatchVO.class);
    }

    @Override
    public boolean save(DeliveryTaskBatchDTO dto) {
        DeliveryTaskBatch deliveryTaskBatch = BeanUtil.copyProperties(dto, DeliveryTaskBatch.class);
        // 批次编号
        // 状态：待审核
        deliveryTaskBatch.setStatus(BatchStatusEnum.APPROVAL.getType());
        baseMapper.insert(deliveryTaskBatch);
        return Boolean.TRUE;
    }

    @Override
    public DeliveryTaskBatchInfoVO detail(String id) {
        DeliveryTaskBatchInfoVO vo = new DeliveryTaskBatchInfoVO();
        // 根据批次id查询任务信息，批次信息
        DeliveryTaskBatch deliveryTaskBatch = baseMapper.selectById(id);
        DeliveryTaskBatchVO batchVO = new DeliveryTaskBatchVO();
        BeanUtil.copyProperties(deliveryTaskBatch, batchVO);
        vo.setTaskBatchVO(batchVO);
        // 任务信息
        DeliveryTask byId = taskService.getById(deliveryTaskBatch.getTaskId());
        DelivertTaskInfoVO delivertTaskInfoVO = generateTask(byId);
        vo.setTaskInfoVO(delivertTaskInfoVO);
        // 汇交结果
        List<DeliveryTaskBatchResult> resultList = resultService.list(new LambdaQueryWrapper<DeliveryTaskBatchResult>()
                .eq(DeliveryTaskBatchResult::getBatchId, id)
                .eq(DeliveryTaskBatchResult::getTaskId, deliveryTaskBatch.getTaskId()));
        List<BatchResultVO> resultVos = BeanUtil.copyToList(resultList, BatchResultVO.class);
        vo.setResultList(resultVos);
        return vo;
    }

    @Override
    public boolean pass(String id) {
        DeliveryTaskBatch taskBatch = baseMapper.selectById(id);
        taskBatch.setStatus(BatchStatusEnum.COMPLETED.getType());
        taskBatch.setCompleteTime(LocalDateTime.now());
        // 状态更新时间
        taskBatch.setStatusUpdateTime(LocalDateTime.now());
        baseMapper.updateById(taskBatch);
        return true;
    }

    private DelivertTaskInfoVO generateTask(DeliveryTask task){
        DelivertTaskInfoVO vo = new DelivertTaskInfoVO(task);
        // todo:查找接收单位
        return vo;
    }
}
